<template>
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="1000px"
    @close="close"
    append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单信息" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入订单信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="approveStatus" v-if="!queryParams.overdueStatus">
        <el-select v-model="queryParams.approveStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.approve_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="orderTable" v-loading="loading" max-height="500px" :data="list"  row-key="id" @selection-change="handleSelectionChange">
      <el-table-column v-if="multiple" type="selection" width="50" align="center" :reserve-selection="true"/>
      <el-table-column v-else label="" width="50" align="center">
        <template slot-scope="scope">
          <el-radio class="radio-hide-label" v-model="tableRadio" :label="JSON.stringify(scope.row)" @change="handleSelectionChange"></el-radio>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="name" min-width="120"/>
      <el-table-column label="客户手机号" align="center" prop="phone" min-width="120"/>
      <el-table-column label="申请金额" align="center" prop="applyAmount"  width="120"/>
      <el-table-column label="申请期数" align="center" prop="periodNum" :formatter="row=>row.periodNum+'期'" width="100"/>
      <el-table-column label="申请产品" align="center" prop="productName"  min-width="150"/>
      <el-table-column label="申请时间" align="center" prop="applyTime"  min-width="160"/>
      <el-table-column label="订单状态" align="center" min-width="120">
        <template slot-scope="scope">
          <el-tag size="small" :type="scope.row.approveStatus==12?'success':'danger'">{{selectDictLabel(dict.type.approve_status,scope.row.approveStatus) || '未知状态'}}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        layout="total, prev, pager, next"
        @pagination="getList"
      />
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listForeseeOrder } from "@/api/orderManage/foreseeOrder";
export default {
  name: "SelectOrderDialog",
  dicts: ['approve_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '选择订单',
    },
    queryData: {
      type: Object,
      default: ()=>{},
    }
  },
  data() {
    return {
      show: false,
      // 单选
      tableRadio: '',
      // 选中数组
      ids: [],
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        approveStatus: undefined
      },
      total: 0,
      list: []
    };
  },
  watch:{
    visible(){
      this.show = this.visible
      if(this.show){
        this.$refs.orderTable && this.$refs.orderTable.clearSelection()
        this.tableRadio = ''
        this.ids = []
        this.queryParams.keyword = undefined
        this.queryParams.approveStatus = undefined
        this.handleQuery()
      }
    }
  },
  created(){
    Object.assign(this.queryParams,this.queryData)
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true;
      listForeseeOrder(this.queryParams).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 选中数据
    handleSelectionChange(selection) {
      this.ids = this.multiple?selection:[JSON.parse(selection)];
    },
    close(){
      this.$emit('update:visible', false)
      this.$emit('close')
    },
    confirm(){
      if(this.ids.length){
        this.$emit('update:visible', false)
        this.$emit('confirm',this.ids)
      }else{
        this.$modal.msgWarning("您未选择订单信息！");
      }
    }
  }
};
</script>